package main

import "fmt"

func main() {
	// 打印内存地址
	var a = 10
	fmt.Printf("变量的地址：%x\n", &a)

	var ip *int     /* 指向整型 */
	var fp *float32 /* 指向浮点型 */

	fmt.Printf("ip的值为：%x\n", ip)
	fmt.Printf("fp的值为：%x\n", fp)

	// 空指针
	var b = ip == nil
	fmt.Println(b)

	c := []int{10, 100, 200}
	var ptr [3]*int
	for i := 0; i < 3; i++ {
		ptr[i] = &c[i]
	}

	for i := 0; i < 3; i++ {
		fmt.Printf("a[%d] = %d\n", i, *ptr[i])
	}

	// 指针的指针
	var d int
	var e *int
	var f **int

	d = 300
	e = &d
	f = &e

	fmt.Printf("变量d = %d\n", d)
	fmt.Printf("指针变量e = %d\n", *e)
	fmt.Printf("指针的指针变量 f = %d\n", **f)

	// 指针作为参数传递
	g := 100
	h := 200
	swap1(&g, &h)
	fmt.Printf("交换后g的值：%d\n", g)
	fmt.Printf("交换后h的值：%d\n", h)

	// 普通交换
	m := 100
	n := 200
	swap2(m, n)
	fmt.Printf("交换后m的值：%d\n", m)
	fmt.Printf("交换后n的值：%d\n", n)
}

func swap1(x *int, y *int) {
	var temp int
	temp = *x
	*x = *y
	*y = temp
}

func swap2(x int, y int) {
	var temp int
	temp = x
	x = y
	y = temp
}
